﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading;
using A.HTMlServer;
namespace WF.lengxiaohua
{
    public partial class LXForm : Form
    {
        public LXForm()
        {
            InitializeComponent();
        }

        private List<string> Llink = new List<string>();
        private A.HTMlServer.DCommon dc = new A.HTMlServer.DCommon();
        private object objState = new object();
        private bool ActiveState = false;
        delegate void dfun();
        private int i = 0;
        private int j = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            //http://lengxiaohua.net/sitemap/#2009-05
            Thread Th = new Thread(GetLink);
            Th.Start();

            Thread Tu = new Thread(ThreadUi);
            Tu.Start();
        }

        private void ThreadUi()
        {
            do
            {
                this.Invoke(new dfun(Go));
                Thread.Sleep(1000);
            } while (true);
        }

        private void Go()
        {
            
            progressBar1.Maximum = i;
            progressBar1.Value = j;
            label1.Text = "下载完成"+j+"/"+i+"个";
            
        }

        private void GetLink()
        {
            //string url = "http://lengxiaohua.net/2008/12/11/time-machine/";
            //string Html = dc.GetStrHtml(url);
            //GetPageHtml(Html);
            string url = "http://lengxiaohua.net/sitemap/";
            string Html = dc.GetStrHtml(url);
            if (Html != null)
            {
                GetA(Html);
            }
        }


        
        private void ThreadRun()
        {
            string strUrl=null;
            do{
                lock(objState)
                {
                    if (Llink.Count > 0)
                    {
                        strUrl = Llink[0];
                        Llink.RemoveAt(0);
                    }
                }
                strUrl = dc.GetStrHtml(strUrl);
                if (strUrl != null)
                {
                    GetPageHtml(strUrl);
                }
                Thread.Sleep(500);
            }while(true);
        }

        private void Threadstart()
        {
            for (var i = 0; i < 10;i++ )
            {
                Thread t = new Thread(ThreadRun);
                t.Start();
            }
        }

        private void GetA(string Html)
        {
            //http://lengxiaohua.net/2008/04/12/not-always/
            List<string> Ltemp = new List<string>();
            Regex R = new Regex(@"http://lengxiaohua.net/200\d/\d{1,2}/\d{1,2}/.*?/",RegexOptions.IgnoreCase);
            MatchCollection MC = R.Matches(Html);
            foreach(Match m in MC)
            {
                if (!Ltemp.Contains(m.Value))
                {
                    Ltemp.Add(m.Value);
                    i++;
                    lock (objState)
                    {
                        Llink.Add(m.Value);
                    }
                    if(!ActiveState)
                    {
                        Threadstart();
                        ActiveState = true;
                    }
                }
                Thread.Sleep(500);
            }
        }

        private void GetPageHtml(string Html)
        {
            ArticleModel model = new ArticleModel();
            model.Title = ToTitle(Html);

            model.Content = ToContent(Html);
            model.page = 1;

            model.Introduction = "";

            model.Date = ToDate(Html);
            model.From = "我们爱讲冷笑话";

            model.Code = "14";
            model.CodeName = "冷笑话";

            A.HTMlServer.ArticleData.DatatoSqlArticle(model);
            j++;
            //return model;
        }

        private string ToTitle(string Html)
        {
            //<h2 class="entry-title"><a href="http://lengxiaohua.net/2009/05/14/thank-you/" rel="bookmark" title="Permanent Links to 传说中好心情发生器">传说中好心情发生器</a></h2>
            return dc.GetPartHtml(Html, "<h2.*?><a.*?>(.*?)</a></h2>",1);
        }
        private string ToContent(string Html)
        {
            //<div class="entry-content">......<p><a title="最好玩的小游戏"
            string Content = dc.GetPartHtml(Html, "<div class=\"entry-content\">(\\s*<p.*?分享</p>)?(.*?)<p><a title=\"最好玩的小游戏\"", 2);
            return dc.RemoveA(Content).Replace("http://lengxiaohua.org/", ""); ;
        }
        private string ToIntroduction(string Html)
        {
            return "";
        }
        private string ToFrom(string Html)
        {
            return "我们爱讲冷笑话";
        }
        private DateTime ToDate(string Html)
        {
            string str=dc.GetPartHtml(Html,@"http://lengxiaohua.net/(200\d/\d{2}/\d{2})/",1);
            DateTime dt;
            if (DateTime.TryParse(str, out dt))
            {
                return dt;
            }
            return DateTime.Now;
        }
    }
}
